package com.yami.shop.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Brand;
import com.yami.shop.bean.model.Museum;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 博物馆管理
 *
 * @author lsr
 */
@RestController
@RequestMapping("/platform/museum")
@Tag(name="博物馆相关接口")
public class MuseumController {

    @Autowired
    private MuseumService museumService;

    @GetMapping("/page")
    @Operation(summary =  "分页获取博物馆信息列表", description = "分页获取博物馆信息列表")
    public ServerResponseEntity<IPage<Museum>> page(PageParam<Museum> pageDTO, @ParameterObject Museum brandDTO) {
        QueryWrapper<Museum> museumQueryWrapper = new QueryWrapper<>();
        museumQueryWrapper.like(ObjectUtil.isNotEmpty(brandDTO.getMuseumName()),"museum_name",brandDTO.getMuseumName());
        museumQueryWrapper.orderByDesc("create_time");
        IPage<Museum> brandPage = museumService.page(pageDTO,museumQueryWrapper);
        return ServerResponseEntity.success(brandPage);
    }

    @GetMapping("/info/{brandId}")
    @Operation(summary =  "获取博物馆信息", description = "根据Id获取博物馆信息")
    @Parameter(name = "id", description = "博物馆id")
    public ServerResponseEntity<Museum> getByBrandId(@PathVariable Long brandId) {
        Museum museum = museumService.getById(brandId);
        return ServerResponseEntity.success(museum);
    }

    @PostMapping
    @Operation(summary =  "保存博物馆信息", description = "保存博物馆信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody Museum museum) {
        museum.setStatus(1);
        museumService.save(museum);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary =  "更新博物馆信息", description = "更新博物馆信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody Museum museum) {
        museumService.updateById(museum);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary =  "删除博物馆信息", description = "根据博物馆id删除博物馆信息")
    @Parameter(name = "id", description = "博物馆id")
    public ServerResponseEntity<Void> delete(@RequestParam Long brandId) {
        museumService.removeById(brandId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list")
    @Operation(summary =  "获取博物馆列表", description = "获取博物馆列表")
    public ServerResponseEntity<List<Museum>> getMuseumList() {
        return ServerResponseEntity.success(museumService.list(new QueryWrapper<Museum>().orderByDesc("create_time")));
    }

}
